import request from '@/utils/request'
envConfig.uploadPath = process.env.VUE_APP_URL + process.env.VUE_APP_UPLOAD_PATH
export function page(title, estateId, begin, end, page, pageSize) {
  return request({
    url: process.env.VUE_APP_PATH + '/newhouses?title=' + title + '&estateId=' + estateId + '&begin=' + begin + '&end=' + end + '&page=' + page + '&pageSize=' + pageSize,
    method: 'get'
  }).then(res => {
    if (res.data.code === 1) {
      const rows = res.data.data.rows
      for (const i in rows) {
        if (rows[i].houseImages) {
          const images = rows[i].houseImages.split(',')
          for (var l in images) {
            if (images[l] !== '') {
              rows[i].houseImages = envConfig.uploadPath + images[l]
              break
            }
          }
        }
      }
      return res
    }
  })
}

export function add(emp) {
  return request({
    url: process.env.VUE_APP_PATH + '/newhouses',
    method: 'post',
    data: emp
  })
}

export function update(dept) {
  return request({
    url: process.env.VUE_APP_PATH + '/newhouses',
    method: 'put',
    data: dept
  })
}

export function selectById(id) {
  return request({
    url: process.env.VUE_APP_PATH + '/newhouses/' + id,
    method: 'get'
  }).then(res => {
    const data = res.data.data[0]
    res.data.data = data
    if (data.houseImages) {
      const images = data.houseImages.split(',')
      const houseImagesArr = []
      for (var l in images) {
        if (images[l] !== '') {
          houseImagesArr.push({ name: l, url: envConfig.uploadPath + images[l] })
        }
      }
      res.houseImagesArr = houseImagesArr
    }
    return res
  })
}

export function deleteById(id) {
  return request({
    url: process.env.VUE_APP_PATH + '/newhouses/' + id,
    method: 'delete'
  })
}

export function deleteImages(imagesList) {
  return request({
    url: process.env.VUE_APP_PATH + '/deleteImages',
    method: 'delete',
    data: {
      imagesList: imagesList
    }
  })
}
